<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Model\AdminNodeModel;
use App\Model\AdminRoleModel;
use DB;

/**
 * 角色Role控制器
 */
class AdminRole extends Controller
{
    /**
     * 显示角色列表页面
     */
    public function index()
    {
        //
        $roles = AdminRoleModel::select('id', 'name', 'desc')->get();

        return view('Admin.adminUser.admin-role', ['roles' => $roles]);
    }

    /**
     * 显示添加角色页面
     */
    public function create()
    {
        //
        $nodes = AdminNodeModel::select('id', 'name', 'desc')->get();

        return view('Admin.adminUser.admin-role-add', ['nodes' => $nodes]);
    }

    /**
     * Store a newly created resource in storage.
     * 处理添加角色
     */
    public function store(Request $request)
    {
        //
         $this->validate($request, [
            'name' => 'required',
            'desc' => 'required',
            'node' => 'required',
        ],[
            'name.required' => '角色名不能为空哦！',
            'desc.required' => '描述不可以为空哦！',
            'node.required' => '权限节点不能为空哦！',
        ]);

        $role = new AdminRoleModel();

        $role->name = $request->input('name');
        $role->desc = $request->input('desc');

        $role->save();

        $node_role = [];
        foreach ($request->input('node') as $value) {
            $node_role[] = [
                'node_id' => $value,
                'role_id' => $role->id
            ];
        }
        DB::table('shop_admin_node_roles')->insert($node_role);

        return back()->with('message', '角色添加成功');

    }

    /**
     * Display the specified resource.
     * 显示角色详情
     */
    public function show($id)
    {

        $role = AdminRoleModel::find($id);

        return view('Admin.adminUser.admin-role-show', ['role' => $role]);
    }

    /**
     * Show the form for editing the specified resource.
     * 显示编辑角色页面
     */
    public function edit($id)
    {
        //
        $role = AdminRoleModel::find($id);
        $nodes = AdminNodeModel::all()->toArray();
        $roleNode = $role->nodes;

        //求出所有节点与该角色拥有的节点的交集
        foreach($nodes as $node) {

          $q[] = $node['id'];

        }
        foreach($roleNode as $v) {

          $w[] = $v['id'];

        }

        $inter = array_intersect($q, $w);

        return view('Admin.adminUser.admin-role-edit', ['role' => $role, 'nodes' => $nodes, 'inter' => $inter]);

    }

    /**
     * Update the specified resource in storage.
     * 处理编辑角色
     */
    public function update(Request $request, $id)
    {
        //
        // 验证数据
        $this->validate($request, [
            'name' => 'required|max:191',
            'desc' => 'required',
            'node' => 'required',
        ],[
            'name.required' => '角色名不能为空哦！',
            'desc.required' => '描述不可以为空哦！',
            'node.required' => '权限节点不能为空哦！',
        ]);

        // 存储数据
        $role = AdminRoleModel::find($id);
        $role->desc = $request->input('desc');
        $role->name = $request->input('name');

        $role->update();

        // 分配节点
        DB::table('shop_admin_node_roles')->where('role_id', $id)->delete();
        $node_role = [];
        foreach ($request->input('node') as $value) {
            $node_role[] = [
                'node_id' => $value,
                'role_id' => $role->id
            ];
        }
        DB::table('shop_admin_node_roles')->insert($node_role);

        return back()->with('message', '角色修改成功');
    }

    /**
     * Remove the specified resource from storage.
     * 处理删除角色
     */
    public function destroy(Request $request)
    {
        //
        $res = AdminRoleModel::where('id', $request->id)->delete();

        if ($res) {

            return response()->json($res);
        }

        return back()->with('message', '删除失败');
    }
}
